/**
 * Copyright (C), 2015-2019, bogewang
 * FileName: LoginController
 * Author:   foren236598164@aliyun.com
 * Date:     2019/9/18 14:13
 * Description: 会员登录
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bogewang.member.controller;

import com.alibaba.fastjson.JSONObject;
import com.bogewang.core.base.BaseResponse;
import com.bogewang.core.bean.BeanTransferUtils;
import com.bogewang.core.constants.Constants;
import com.bogewang.member.controller.req.vo.LoginVO;
import com.bogewang.member.entity.LoginDTO;
import com.bogewang.member.feign.MemberLoginServiceFeign;
import com.bogewang.web.base.BaseWebController;
import com.bogewang.web.constants.WebConstants;
import com.bogewang.web.utils.CookieUtils;
import com.bogewang.web.utils.RandomValidateCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 〈会员登录〉
 *
 * @author foren236598164@aliyun.com
 * @create 2019/9/18
 * @since 1.0.0
 */
@Controller
public class LoginController extends BaseWebController {
    private static final String MB_LOGIN_PAGE = "member/login";
    @Autowired
    private MemberLoginServiceFeign memberLoginServiceFeign;

    /**
     * 重定向首页;
     */
    private static final String REDIRECT_INDEX = "redirect:/";

    /**
     * 跳转到注册页面
     *
     * @return
     */
    @GetMapping("/login")
    public String getLogin() {
        return MB_LOGIN_PAGE;
    }

    /**
     * 跳转到注册页面
     *
     * @return
     */
    @PostMapping("/login")
    public String postLogin(@ModelAttribute("loginVo") LoginVO loginVO, Model model, HttpServletRequest request,
                            HttpServletResponse response, HttpSession httpSession) {
        //1.图形验证码验证;
        String graphicCode = loginVO.getGraphicCode();
        if (!RandomValidateCodeUtil.checkVerify(graphicCode, httpSession)){
            setErrorMsg(model, "图形验证码不正确!");
        }

        //2.将VO装换为dto, 调用会员登录接口;
        LoginDTO loginDTO = getLoginDTO(loginVO, request);
        BaseResponse<JSONObject> login = memberLoginServiceFeign.login(loginDTO);
        if (!isSuccess(login)){
            setErrorMsg(model, login.getRtnMsg());
            return MB_LOGIN_PAGE;
        }

        //3.处理会话
        return redirectIndex(request, response, login);
    }

    private LoginDTO getLoginDTO(@ModelAttribute("loginVo") LoginVO loginVO, HttpServletRequest request) {
        LoginDTO loginDTO = BeanTransferUtils.EToT(loginVO, LoginDTO.class);
        loginDTO.setLoginType(Constants.MEMBER_LOGIN_TYPE_PC);
        String browserInfo = webBrowserInfo(request);
        loginDTO.setDeviceInfo(browserInfo);
        return loginDTO;
    }

    /**
     * 登出;
     * @return
     */
    @RequestMapping("/exit")
    public String exit(@ModelAttribute("loginVo") LoginVO loginVO, Model model,HttpServletRequest request
            ,HttpServletResponse response, HttpSession httpSession){
        //1.数据库token置为0
        String token  = CookieUtils.getCookieValue(request, response, WebConstants.LOGIN_TOKEN_COOKIENAME, true);
        BaseResponse<JSONObject> loginOut = memberLoginServiceFeign.loginOut(token);
        if (!isSuccess(loginOut)){
            setErrorMsg(model, "登出失败!");
            return REDIRECT_INDEX;
        }

        //2.清楚缓存;
        CookieUtils.deleteCookie(request, response, WebConstants.LOGIN_TOKEN_COOKIENAME);

        //3.跳转首页;
        return MB_LOGIN_PAGE;
    }

}